import os
from random import choice
# from requests import get

INDEX_HTML = "/home/plaza/server/index.html"
SERVER_PATH = "/home/plaza/server/a/"

RANDOM_SIZE = 5
RANDOM_ALPHABET = (
    "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    "0123456789"
)


def gen_random(size, alphabet) -> str:
    out = ''
    for _ in range(size):
        out += choice(alphabet)
    return out


def main() -> None:
    images = next(os.walk(SERVER_PATH), (None, None, []))[2]
    # images = list(map(int, [image[3:-4] for image in images]))
    images = [image[:-4] for image in images]
    # out = max(images) + 1 if images else 0
    name = ''
    while True:
        name = gen_random(RANDOM_SIZE, RANDOM_ALPHABET)
        if name not in images:
            break

    # os.system(
    #     f"flameshot gui --raw > {SERVER_PATH}img{out}.png"
    # )

    os.system(
        f"flameshot gui --raw > {SERVER_PATH}{name}.png"
    )

    ip = "95.54.38.170"  # get('https://api.ipify.org').content.decode('utf8')

    os.system(
        f"echo \"http://{ip}:8000/a/{name}.png\" |"
        f" xclip -selection clipboard"
    )

    with open(INDEX_HTML, 'w') as f:
        f.write(f"""
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    http://{ip}:8000/a/{name}.png
</body>
</html>""")


if __name__ == '__main__':
    main()
